﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_DLL.Product.database;
using ERP_RORZE_DLL.Product.DAO;
using ERP_RORZE_DLL.Product.Base;

namespace ERP_RORZE_PRODUCT.Product.PartData
{
    public partial class Frm_OptionDataAdd : DevExpress.XtraEditors.XtraForm
    {
        ClsOptionDataTable obj = new ClsOptionDataTable();
        ClsPartDataDAO clspartDAO = new ClsPartDataDAO();
        DataTable dt;
        public EventHandler Re_Load;
        public Frm_OptionDataAdd()
        {
            InitializeComponent();
        }

        void ThongBao(string name)
        {
            MessageBox.Show("Chưa nhập " + name + ". Vui lòng nhập lại");
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {

            if (txtClampTime.Text == "")
            {
                ThongBao(clsConstantsdb.Entities.ClsDBOptionData.FieldName.ClampTime);
            }
            else if (txtProTime.Text == "")
            {
                ThongBao(clsConstantsdb.Entities.ClsDBOptionData.FieldName.ProTime);
            }
            else if (txtToolQty.Text == "")
            {
                ThongBao(clsConstantsdb.Entities.ClsDBOptionData.FieldName.ToolQty);
            }
            else
            {
                if (txtAondNote.Text == "")
                {
                    txtAondNote.Text = "0";
                }
                try
                {
                    
                    float.Parse(txtAondNote.Text);
                    float.Parse(txtClampTime.Text);
                    float.Parse(txtProTime.Text);
                    int.Parse(txtToolQty.Text);

                    if (dateCLUpdateday.EditValue == null)
                    {
                        dateCLUpdateday.DateTime = DateTime.Parse("1753-01-01 00:00:00.000");
                    }

                    if (dateUpdateDay.EditValue == null)
                    {
                        dateUpdateDay.DateTime = DateTime.Parse("1753-01-01 00:00:00.000");
                    }
                    if (cbStaffID.Text == "")
                    {
                        MessageBox.Show(cbStaffID.SelectedValue.ToString());
                    }
                    obj.InputDate = dateInputDate.Value;
                    obj.PartID = cbPartID.Text;
                    obj.MachineID = cbMachineID.Text;
                    obj.OptionID = cbOptionID.Text;
                    obj.JigID = cbJigID.Text;
                    obj.StaffID = cbStaffID.SelectedValue.ToString();
                    obj.ToolQty = int.Parse(txtToolQty.Text);
                    obj.ProTime = float.Parse(txtProTime.Text);
                    obj.ClampTime = float.Parse(txtClampTime.Text);
                    obj.TechDate = checkTechDate.Checked;
                    obj.UpdateDay = dateUpdateDay.DateTime;
                    obj.Note = txtNote.Text;
                    obj.AondNote = float.Parse(txtAondNote.Text);
                    obj.Memo = txtMemo.Text;
                    obj.CLUpdateday = dateCLUpdateday.DateTime;
                    obj.LastOption = checkLastOption.Checked;
                    obj.Jig = checkJig.Checked;
                    obj.JigType = txtJigType.Text;

                    //MessageBox.Show("" + dateUpdateDay.EditValue + ",,,,,," + dateCLUpdateday.EditValue);
                    //MessageBox.Show(""+dateUpdateDay.DateTime+",,,,,,"+dateCLUpdateday.DateTime);

                    clspartDAO.InsertOptionData(obj);
                    MessageBox.Show("Đã nhập xong");
                    this.Re_Load(sender, e);
                }
                catch
                {
                    MessageBox.Show("AondNote, ClampTime, ProTime, ToolQty phải là kiểu số");
                }

            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Frm_OptionDataAdd_Load(object sender, EventArgs e)
        {
            //lấy PartID  PartID
            
            dt = clspartDAO.GetAllPartID();
            DataRow r1 = dt.NewRow();
            r1["PartNo"] = "";
            r1["PartNo"] = "";
            dt.Rows.InsertAt(r1, 0);
            cbPartID.DataSource = dt;
            cbPartID.DisplayMember = "PartNo";
            cbPartID.ValueMember = "PartNo";

            // lấy StaffID
            DataTable dt1;
            dt1 = clspartDAO.GetAllStaffID();
            DataRow r = dt1.NewRow();
            r["FullName"] = "";
            r["StaffID"] = "";
            dt1.Rows.InsertAt(r, 0);
            cbStaffID.DataSource = dt1;
            cbStaffID.DisplayMember = "FullName";
            cbStaffID.ValueMember = "StaffID";

            // lấy OptionID
            DataTable dt2;
            dt2 = clspartDAO.GetAllOptionID();
            DataRow r2 = dt2.NewRow();
            r2["OptionID"] = "";
            r2["OptionID"] = "";
            dt2.Rows.InsertAt(r2, 0);
            cbOptionID.DataSource = dt2;
            cbOptionID.DisplayMember = "OptionID";
            cbOptionID.ValueMember = "OptionID";

            //
            DataTable dt3;
            dt3 = clspartDAO.GetAllMachineID();
            DataRow r3 = dt3.NewRow();
            r3["MachineID"] = "";
            r3["MachineID"] = "";
            dt3.Rows.InsertAt(r3, 0);
            cbMachineID.DataSource = dt3;
            cbMachineID.DisplayMember = "MachineID";
            cbMachineID.ValueMember = "MachineID";
            
            //Lấy jigID
            DataTable dt4;
            dt4 = clspartDAO.GetAllJigID();
            DataRow r4 = dt4.NewRow();
            r4["JigName"] = "";
            r4["JigName"] = "";
            dt4.Rows.InsertAt(r4, 0);
            cbJigID.DataSource = dt4;
            cbJigID.DisplayMember = "JigName";
            cbJigID.ValueMember = "JigName";

        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            dateCLUpdateday.EditValue = null;
            MessageBox.Show("" + dateCLUpdateday.DateTime);
        }
    }
}